package com.pai4j.message.provider;

import com.pai4j.message.core.provider.AbstractResourceProvider;
import com.pai4j.message.model.ResourceInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 课程资源提供者（示例实现）
 * 提供课程资源信息
 * 
 * <p>当前为示例实现，返回模拟数据。
 * 实际使用时需要注入CourseClient并调用实际的课程服务API。</p>
 *
 * @author PAI4J Team
 * @date 2025-11-01
 */
@Slf4j
@Component
public class CourseResourceProvider extends AbstractResourceProvider {

    // TODO: 注入课程服务Client
    // @Autowired
    // private CourseClient courseClient;

    @Override
    public String getResourceType() {
        return "COURSE";
    }

    @Override
    protected ResourceInfo doGetResourceInfo(String resourceId) {
        if (StringUtils.isBlank(resourceId)) {
            return null;
        }

        try {
            // TODO: 调用实际的课程服务获取课程信息
            // PAIResponseBean<CourseDetailVO> response = courseClient.getCourseDetail(courseId);
            
            // 临时实现：构建模拟数据
            log.debug("获取课程资源信息: courseId={}", resourceId);
            
            // 实际项目中应该这样实现：
            /*
            Long courseId = Long.parseLong(resourceId);
            PAIResponseBean<CourseDetailVO> response = courseClient.getCourseDetail(courseId);
            
            if (!PAIResponseBeanUtil.isOk(response) || response.getData() == null) {
                log.warn("获取课程信息失败: courseId={}", courseId);
                return null;
            }

            CourseDetailVO course = response.getData();
            
            return ResourceInfo.builder()
                    .resourceId(resourceId)
                    .resourceType("COURSE")
                    .title(course.getTitle())
                    .ownerId(course.getTeacherId())
                    .authorName(course.getTeacherName())
                    .coverImage(course.getCoverImage())
                    .summary(course.getIntroduction())
                    .jumpUrl("/course/detail/" + resourceId)
                    .status(course.getStatus())
                    .addExtInfo("duration", course.getDuration())
                    .addExtInfo("studentCount", course.getStudentCount())
                    .addExtInfo("price", course.getPrice())
                    .addExtInfo("categoryId", course.getCategoryId())
                    .build();
            */

            // 临时返回基本信息（待集成实际服务时替换）
            return ResourceInfo.builder()
                    .resourceId(resourceId)
                    .resourceType("COURSE")
                    .title("课程-" + resourceId)
                    .ownerId("teacher-" + resourceId)
                    .authorName("讲师名称")
                    .jumpUrl("/course/detail/" + resourceId)
                    .status(null)
                    .build();

        } catch (Exception e) {
            log.error("获取课程资源信息异常: resourceId={}, error={}", resourceId, e.getMessage(), e);
            return null;
        }
    }

    @Override
    public int getPriority() {
        return 8; // 中高优先级
    }
}

